
 import { Bus } from '@/utils/bus'

 const transferMixin = {
  data() {
    return {
      dialogVisible: false,
      transferCode: '',
      transferValue: [],
      transferRightValueObj: {},
      tenantObj: {},
      goodsAttrChild: {
        valArray: [],
        valArrayName: "",
        transferRightValueObj: {}
      }
    }
  },
  provide() {
    return {
      openGoodsDialog: this.openGoodsDialog
    };
  },
  methods: {
    getGoodsInfo({ paramsArr, transferRightValueObj }) {
      const codes = [];
      const labels = [];
      const { goodsAttrChild } = this;
      paramsArr.forEach((o) => {
        codes.push(o.code);
        labels.push(o.name);
      });
      goodsAttrChild.valArray = codes;
      goodsAttrChild.valArrayName = labels.join(',');
      goodsAttrChild.transferRightValueObj = transferRightValueObj;
      Bus.$emit('memberChange')
    },
    /**
     * 主要用于ruleTreeNode组件使用。
     * @param {object} child
     */
    openGoodsDialog(child) {
      this.dialogVisible = true
      const { property } = child;
      this.goodsAttrChild = child;
      const { valArray: transferValue, transferRightValueObj } = child
       if (!transferRightValueObj) { // 防止遗漏
        this.$set(child, 'transferRightValueObj', {})
      }
      this.transferCode = property[property.length - 1]
      this.transferValue = transferValue
      this.transferRightValueObj = transferRightValueObj
    },
    /** *
     * @param transferCode 后端接受的code
     * @param transferValue 穿梭框所有选中项的集合,code的集合
     */
    openTransferDialog({ transferCode }) {
      this.dialogVisible = true
      this.transferCode = transferCode
      const keyCode = `${transferCode}Codes`
      this.transferValue = this.searchData[keyCode] ? this.searchData[keyCode].split(',') : []
    },
    /**
     *
     * @param code 穿梭框选中的值
     * @param name 穿梭框选中的值的名称
     */
    getTransferInfo({ code, name, type, transferRightValueObj }) {
      const { searchData } = this
      const keyCode = `${type}Codes`
      const keyName = `${type}Name`
      searchData[keyCode] = code
      searchData[keyName] = name
      searchData['transferRightValueObj'] = transferRightValueObj
    },
    // 自动化营销群组使用 --可跟上面的openTransferDialog 合并优化。时间原因，暂时搁置。
    getTenantInfo({ code, name, type, transferRightValueObj }) {
      const keyCode = `${type}Codes`
      const keyName = `${type}Name`
      this.tenantObj[keyCode] = code
      this.tenantObj[keyName] = name
      if ('name' in this.tenantObj) {
        this.tenantObj['name'] = name
      }
      this.transferRightValueObj = transferRightValueObj
    },
    openTenantDialog(node, type) {
      this.dialogVisible = true
      this.tenantObj = node
      this.transferValue = node[type] ? node[type].split(',') : []
    }
  }
}
export { transferMixin }
